package com.stylefeng.guns.modular.drugstype.controller;


import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.constant.tips.HasDurgsTip;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.DrugsTypeMapper;
import com.stylefeng.guns.common.persistence.model.Drugs;
import com.stylefeng.guns.common.persistence.model.DrugsType;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.drugs.service.impl.DrugsServiceImpl;
import com.stylefeng.guns.modular.drugstype.dao.DrugstypeDao;
import com.stylefeng.guns.modular.drugstype.service.IDrugstypeService;
import com.stylefeng.guns.modular.system.warpper.DrugstypeWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * drugstype控制器
 *
 * @author fengshuonan
 * @Date 2019-07-12 10:27:32
 */
@Controller
@RequestMapping("/drugstype")
public class DrugstypeController extends BaseController {
    @Resource
    DrugstypeDao drugstypeDao;
    @Resource
    DrugsTypeMapper drugsTypeMapper;
    @Resource
    IDrugstypeService drugstypeService;

    private String PREFIX = "/drugstype/drugstype/";

    /**
     * 跳转到drugstype首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "drugstype.html";
    }

    /**
     * 跳转到添加drugstype
     */
    @RequestMapping("/drugstype_add")
    public String drugstypeAdd() {
        return PREFIX + "drugstype_add.html";
    }

    /**
     * 跳转到修改drugstype
     */
    @RequestMapping("/drugstype_update/{id}")
    public String drugstypeUpdate(@PathVariable Integer id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        DrugsType drugsType=drugsTypeMapper.selectById(id);
        model.addAttribute(drugsType);
        LogObjectHolder.me().set(drugsType);
        return PREFIX + "drugstype_edit.html";
    }

    /**
     * 获取drugstype列表
     */
    @ApiOperation("药品类别信息")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(@RequestParam(required = false) String condition) {
        List<Map<String, Object>> list = this.drugstypeDao.list(condition);
        return super.warpObject(new DrugstypeWrapper(list));
    }

    /**
     * 新增drugstype
     */
    @ApiOperation("新增类别")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add( @ModelAttribute @Valid DrugsType drugsType) {
        drugsTypeMapper.insert(drugsType);
        return super.SUCCESS_TIP;

    }

    /**
     * 删除drugstype
     */
    @ApiOperation("删除类别")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam(required = false) Integer id) {
        int flag=drugstypeService.IfDrugsType(id);
        if(flag>0){
            return ERROR_MSG;
        }
        else{
            drugsTypeMapper.deleteById(id);
            return SUCCESS_TIP;
        }
    }
    /**
     * 修改drugstype
     */
    @ApiOperation("修改类别")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@ModelAttribute @Valid DrugsType drugsType) {
        if (ToolUtil.isOneEmpty(drugsType, drugsType.getId())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        DrugsType drugsType1=this.drugsTypeMapper.selectById(drugsType.getId());
        drugsType1.setTypename(drugsType.getTypename());
        //drugstypeService.updateDrugsType(drugsType1);
        drugsTypeMapper.updateById(drugsType1);
        return SUCCESS_TIP;
    }

    /**
     * drugstype详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public Object detail() {
        return null;
    }
}
